﻿/* 2014/7/3 */
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Cosmos.Media.Play
{
    /// <summary>
    /// Represents a collection of objects of PlayAttribute class.
    /// </summary>
    public class PlayAttributeCollection : IEnumerable<PlayAttribute>, ICollection<PlayAttribute>, IList<PlayAttribute>
    {
        private Collection<PlayAttribute> _attributes;

        private IPlayEntity _entity;

        /// <summary>
        /// Initializes a new instance of PlayAttributeCollection class.
        /// </summary>
        protected internal PlayAttributeCollection(IPlayEntity entity)
        {
            _attributes = new Collection<PlayAttribute>();

            _entity = entity;
        }

        public void Add(PlayAttribute attribute)
        {
            attribute.Entity = _entity;
            _attributes.Add(attribute);
        }

        public void AddRange(IEnumerable<PlayAttribute> attributes)
        {
            foreach (PlayAttribute anAttribute in attributes)
            {
                anAttribute.Entity = _entity;
                _attributes.Add(anAttribute);
            }
        }

        public void Clear()
        {
            _attributes.Clear();
        }

        public bool Contains(PlayAttribute attribute)
        {
            return _attributes.Contains(attribute);
        }

        public void CopyTo(PlayAttribute[] array, int index)
        {
            _attributes.CopyTo(array, index);
        }

        public IEnumerator<PlayAttribute> GetEnumerator()
        {
            return _attributes.GetEnumerator();
        }

        public int IndexOf(PlayAttribute attribute)
        {
            return _attributes.IndexOf(attribute);
        }

        public void Insert(int index, PlayAttribute attribute)
        {
            attribute.Entity = _entity;
            _attributes.Insert(index, attribute);
        }

        public bool Remove(PlayAttribute attribute)
        {
            if (_attributes.Contains(attribute))
            {
                attribute.Entity = null;
            }
            return _attributes.Remove(attribute);
        }

        public void RemoveAt(int index)
        {
            if (index >= 0 && index < _attributes.Count)
            {
                PlayAttribute attribute = _attributes[index];
                attribute.Entity = null;
            }
            _attributes.RemoveAt(index);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return (IEnumerator)_attributes.GetEnumerator();
        }

        public int Count
        {
            get
            {
                return _attributes.Count;
            }
        }

        bool ICollection<PlayAttribute>.IsReadOnly
        {
            get
            {
                return ((ICollection<PlayAttribute>)_attributes).IsReadOnly;
            }
        }

        public PlayAttribute this[int index]
        {
            get
            {
                return _attributes[index];
            }

            set
            {
                _attributes[index] = value;
            }
        }

        public IPlayEntity Entity
        {
            get
            {
                return _entity;
            }
        }

        protected Collection<PlayAttribute> Attributes
        {
            get
            {
                return _attributes;
            }

            set
            {
                _attributes = value;
            }
        }
    }
}
